<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2019/9/23 0023
 * Time: 上午 9:47
 */

namespace app\wxsite\controller;
use app\dlc\common\helpers\ApiHelper;
use think\Db;

class planController
{
    //执行计划任务
    public function index(){
        $data = Db::name('plan')->where('status',1)->select();
        foreach ($data as $key => $value) {
//            时间段
            $period = explode(',',$value['period']);
            $h = date('h',time());
            if (in_array($h,$period)){
                $num = ceil($value['num']/count($period));//获取文本的数量
                $website = Db::name('website')->where('id',$value['website_id'])->find();
                $res = Db::name('article')->where(['status'=>2,'website_id'=>$value['website_id']])->limit($num)->column('id');
                ApiHelper::sendArticle($website['admin_url'],$res);
            }

        }
    }

    //测试
    public function test(){
        $id = input('id');
        ApiHelper::sendArticle('http://kang.batterywd.com',[$id]);
    }

    public function getCookie(){
        $http = 'http://sys.batterywd.com/dlc/public/login.html';
        $data['username'] = "admin";
        $data['userpass'] = "123456";
        $data['no_check'] = "1";
        $re = $this->post_curl($http, $data, 1);
        // 解析HTTP数据流
        list($header, $body) = explode("\r\n\r\n", $re);
        // 解析COOKIE
        preg_match("/set\-cookie:([^\r\n]*)/i", $header, $matches);
        //请求的时候headers 带上cookie就可以了
        $cookie = explode(';', $matches[1])[0];
        echo $cookie;
        /*
         * 模拟post请求
         */
    }
    function post_curl($url, $params=[], $headers=[]){
        $httpInfo = array();
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_HEADER, 1);
        curl_setopt( $ch, CURLOPT_HTTP_VERSION , CURL_HTTP_VERSION_1_1 );
        curl_setopt( $ch, CURLOPT_USERAGENT , 'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/41.0.2272.118 Safari/537.36' );
        curl_setopt( $ch, CURLOPT_CONNECTTIMEOUT , 60 );
        curl_setopt( $ch, CURLOPT_TIMEOUT , 60);
        curl_setopt( $ch, CURLOPT_RETURNTRANSFER , true );

        curl_setopt( $ch , CURLOPT_POST , true );
        curl_setopt( $ch , CURLOPT_POSTFIELDS , http_build_query($params));
        curl_setopt( $ch , CURLOPT_URL , $url );


        $response = curl_exec( $ch );
        if ($response === FALSE) {
            return false;
        }

        curl_close( $ch );
        return $response;
    }


}